<template>
  <div>
    <Table
      ref="tableRef"
      :url="URL_PAGINGQUESTIONCLASS"
      :columns="data.columns"
      :handles="data.handles"
      :toolbar="data.toolbar"
    ></Table>
    <DialogQuestionAdd
      ref="dialogQuestionAddRef"
      @get-data="getData"
    ></DialogQuestionAdd>
  </div>
</template>

<script lang="ts" setup name="questionList">
import Table from '@/components/table/index.vue';
import DialogQuestionAdd from './dialogQuestionAdd.vue';
import {
  URL_PAGINGQUESTIONCLASS,
  deleteQuestionById,
} from '@/service/apis/market';

const router = useRouter();

const tableRef = ref();
const dialogQuestionAddRef = ref();

const emit = defineEmits(['set-update']);
onActivated(() => {
  tableRef.value?.getData();
});

const data = reactive({
  columns: [
    {
      prop: 'questionClassification',
      label: '问答分类',
    },
    {
      prop: 'questionNumber',
      label: '已录入问题数',
      dataType: String,
    },
    {
      prop: 'showPriority',
      label: '展示优先级',
      dataType: String,
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '问答管理',
      handle: (row: any) => {
        console.log('问答管理 ---', row);
        emit('set-update', false);
        router.push({
          name: 'problemList',
          query: { id: row.id, name: row.questionClassification },
        });
      },
    },
    {
      label: '修改',
      permission: 'questionList.editType',
      handle: (row: any) => {
        console.log('编辑---', row);
        dialogQuestionAddRef.value?.show(row);
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'questionList.deleteType',
      handle: (row: any) => {
        ElMessageBox.confirm(
          '删除问题分类将同步删除该分类下的所有常见问答，是否继续？',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(() => {
          deleteQuestionById({
            id: row.id,
          }).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增问答分类',
      permission: 'questionList.addType',
      icon: 'icon-add',
      width: '120px',
      type: 'primary',
      handle: (row: any) => {
        dialogQuestionAddRef.value?.show();
      },
    },
  ],
});

const getData = () => {
  tableRef.value?.getData();
};
</script>